package com.tfxy.hxl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tfxy.hxl.R;
import com.tfxy.hxl.entity.CourseCategory;
import com.tfxy.hxl.service.CourseCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * 课程分类控制器（精简版）
 */
@RestController
@RequestMapping("/course-category")
@Tag(name = "课程分类管理", description = "课程分类核心功能")
public class CourseCategoryController {

    @Autowired
    private CourseCategoryService courseCategoryService;

    /**
     * 1. 获取启用分类列表
     */
    @GetMapping("/enabled")
    @Operation(summary = "获取启用分类列表")
    public R getEnabledCategories() {
        List<CourseCategory> categories = courseCategoryService.getEnabledCategories();
        return R.ok().data("categories", categories);
    }

    /**
     * 2. 分页查询分类
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询分类")
    public R getCategoryPage(
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "分类名称") @RequestParam(required = false) String name,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {

        QueryWrapper<CourseCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);

        if (StringUtils.hasText(name)) {
            queryWrapper.like("name", name);
        }
        if (status != null) {
            queryWrapper.eq("status", status);
        }

        queryWrapper.orderByAsc("sort_order");

        Page<CourseCategory> page = new Page<>(current, size);
        IPage<CourseCategory> result = courseCategoryService.page(page, queryWrapper);

        return R.ok()
                .data("records", result.getRecords())
                .data("total", result.getTotal())
                .data("current", result.getCurrent())
                .data("size", result.getSize());
    }

    /**
     * 3. 创建分类
     */
    @PostMapping("/create")
    @Operation(summary = "创建分类")
    public R createCategory(@RequestBody CourseCategory category) {
        boolean result = courseCategoryService.createCategory(category);
        if (result) {
            return R.ok().message("分类创建成功");
        } else {
            return R.error().message("分类创建失败，可能分类名称已存在");
        }
    }

    /**
     * 4. 更新分类
     */
    @PutMapping("/update")
    @Operation(summary = "更新分类")
    public R updateCategory(@RequestBody CourseCategory category) {
        if (category.getId() == null) {
            return R.error().message("分类ID不能为空");
        }

        boolean result = courseCategoryService.updateCategory(category);
        if (result) {
            return R.ok().message("分类更新成功");
        } else {
            return R.error().message("分类更新失败，可能分类名称已存在");
        }
    }

    /**
     * 5. 删除分类
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除分类")
    public R deleteCategory(@Parameter(description = "分类ID") @PathVariable Long id) {
        boolean result = courseCategoryService.deleteCategory(id);
        if (result) {
            return R.ok().message("分类删除成功");
        } else {
            return R.error().message("分类删除失败");
        }
    }
}
